﻿using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xyz.Core.Data;
using Xyz.Core.Infrastructure;
using Xyz.Core.Infrastructure.Extentions;
using Xyz.Core.Interfaces;
using Xyz.Core.Models;
using Xyz.Core.Models.Filters;

namespace Xyz.Core.Services.AppServices
{
    
    public class HbxxService : IHbxxService
    {
        private readonly FlightContext _context;
        private readonly IMapper _mapper;
        public string axax;
        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="context"></param>
        /// <param name="mapper"></param>
        public HbxxService(FlightContext context, IMapper mapper)
        {
            _mapper = mapper;
            _context = context;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="filters">搜索参数</param>
        /// <returns></returns>
        public async Task<PagedResult<HbxxDto>> SearchAsync(HbxxFilters filters, int id, string flightnum, string homeIcao, string pointIcao, string starting_point, string end, string ac, string type, string dayTime, string starting_pointAirport, string endAirport)
        {
            if (filters == null)
                return new PagedResult<HbxxDto>(1, 0);
            // var query = _context.Hbxxs.Where(item => !item.IsDeleted);
            var query = from s in _context.Hbxxs
                        select s;

            if (filters.keywords.IsNotBlank())
                query = query.Where(s => s.Starting_point.Contains(filters.keywords)
                                         || s.End.Contains(filters.keywords)
                                         || s.Starting_pointAirport.Contains(filters.keywords)
                                         || s.EndAirport.Contains(filters.keywords)
                                         || s.Ac.Contains(filters.keywords)
                                         || s.Type.Contains(filters.keywords)
                                         || s.HomeIcao.Contains(filters.keywords)
                                         || s.PointIcao.Contains(filters.keywords)
                                         || s.DayTime.Contains(filters.keywords)
                                         || s.Flightnum.Contains(filters.keywords));



            if (id != 0)
            {
                query = query.Where(s => s.MID.Equals(id));
            }
            //起点
            if (!String.IsNullOrEmpty(starting_point))
            {
                query = query.Where(s => s.Starting_point.Contains(starting_point));
            }
            //终点
            if (!String.IsNullOrEmpty(end))
            {
                query = query.Where(s => s.End.Contains(end));
            }
            //起点机场
            if (!String.IsNullOrEmpty(starting_pointAirport))
            {
                query = query.Where(s => s.Starting_point.Contains(starting_pointAirport));
            }
            //终点机场
            if (!String.IsNullOrEmpty(endAirport))
            {
                query = query.Where(s => s.End.Contains(endAirport));
            }
            //航空公司
            if (!String.IsNullOrEmpty(ac))
            {
                query = query.Where(s => s.Ac.Contains(ac));
            }
            //机型
            if (!String.IsNullOrEmpty(type))
            {
                query = query.Where(s => s.Type.Contains(type));
            }
            //日期
            if (!String.IsNullOrEmpty(dayTime))
            {
                query = query.Where(s => s.DayTime.Contains(dayTime));
            }
            //航班号
            if (!String.IsNullOrEmpty(flightnum))
            {
                query = query.Where(s => s.Flightnum.Contains(flightnum));
            }
            //ICAO
            if (!String.IsNullOrEmpty(homeIcao))
            {
                query = query.Where(s => s.HomeIcao.Contains(homeIcao));
            }
            //ICAO
            if (!String.IsNullOrEmpty(pointIcao))
            {
                query = query.Where(s => s.PointIcao.Contains(pointIcao));
            }


            return await query.OrderByDescending(item => item.MID)
                .Select(item => new HbxxDto
                {
                    MID = item.MID,
                    Flightnum = item.Flightnum,
                    Ac = item.Ac,
                    ATime = item.ATime,
                    AaTime = item.AaTime,
                    BTime = item.BTime,
                    BbTime = item.BbTime,
                    DayTime = item.DayTime,
                    BbbTime = item.BbbTime,

                    AaaTime = item.AaaTime,

                    End = item.End,
                    HomeIcao = item.HomeIcao,
                    PointIcao = item.PointIcao,
                    FTime = item.FTime,
                    Starting_point = item.Starting_point,
                    Starting_pointAirport=item.Starting_pointAirport,
                    EndAirport = item.EndAirport,
                    Type = item.Type
                }).PagingAsync(filters.page, filters.rows);
        }

        /// <summary>
        /// 历史
        /// </summary>
        /// <param name="filters">搜索参数</param>
        /// <returns></returns>
        public async Task<PagedResult<HbxxDto>> SearchAsyncflightnum(HbxxFilters filters ,string flightnum)
        {
            if (filters == null)
                return new PagedResult<HbxxDto>(1, 0);
            // var query = _context.Hbxxs.Where(item => !item.IsDeleted);
            var query = from s in _context.Hbxxs
                        select s;

           



           
            //航班号
            if (!String.IsNullOrEmpty(flightnum))
            {
                query = query.Where(s => s.Flightnum.Contains(flightnum));
            }


            return await query.OrderByDescending(item => item.MID)
                .Select(item => new HbxxDto
                {
                    MID = item.MID,
                    Flightnum = item.Flightnum,
                    Ac = item.Ac,
                    ATime = item.ATime,
                    AaTime = item.AaTime,
                    BTime = item.BTime,
                    BbTime = item.BbTime,
                    DayTime = item.DayTime,
                    BbbTime = item.BbbTime,
                    AaaTime = item.AaaTime,
                    End = item.End,
                    HomeIcao = item.HomeIcao,
                    PointIcao = item.PointIcao,
                    FTime = item.FTime,
                    Starting_point = item.Starting_point,
                    Starting_pointAirport = item.Starting_pointAirport,
                    EndAirport = item.EndAirport,
                    Type = item.Type
                }).PagingAsync(filters.page, filters.rows);
        }
    }
}
